package com.share.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.share.domain.*;
import com.share.service.CatelogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: share-admin
 * 描述：
 * @author: XLX
 * @create: 2022-12-05 21:11
 **/
@RestController
@RequestMapping("/sys/catelog")
public class CatelogController extends BaseController {

    @Autowired
    CatelogService catelogService;


    @GetMapping("/info/{id}")
    public Result info(@PathVariable(name = "id") Long id){
        Catelog catelog = catelogService.getById(id);

        return Result.succ(catelog);
    }


    @GetMapping("/list")
    public Result list(String name){

        Page<Catelog> page = catelogService.page(getPage(),
                new QueryWrapper<Catelog>().like(StrUtil.isNotBlank(name),"name",name));

        return Result.succ(page);
    }




    @PostMapping("/save")
    public Result save(@Validated @RequestBody Catelog catelog){

        catelogService.save(catelog);
        return Result.succ(catelog);
    }


    @PostMapping("/update")
    public Result update(@Validated @RequestBody Catelog catelog){
        catelogService.updateById(catelog);
        return Result.succ(catelog);
    }


    @PostMapping("/delete")
    @Transactional
    public Result delete(@RequestBody Long[] ids){
        //删除分类
        catelogService.removeByIds(Arrays.asList(ids));

        return Result.succ("分类删除成功");

    }





}